package datasource

import (
	"context"
	"fmt"
	"zzxq/common/setting"

	"github.com/go-redis/redis/v8"
)

var Ctx = context.Background()

var getRedis *redis.Client

func RedisInit() error {
	var (
		redisHost, redisPort, redisAuth string
	)
	var redisDb int

	conf := setting.Config.Redis
	redisHost = conf.Host
	redisPort = conf.Port
	redisAuth = conf.Auth
	redisDb = conf.DB

	getRedis = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%s", redisHost, redisPort),
		Password: redisAuth,
		DB:       redisDb,
	})

	_, err := getRedis.Ping(Ctx).Result()
	if err != nil {
		panic("redis ping error")
	}

	return err
}

//redis 返回redis
func GetRedis() *redis.Client {
	return getRedis
}
